﻿#region Opis pliku
//-----------------------------------------------------------------------------
// Entity.cs
// Autor: Jan Fraś
// 30.10.2012
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace pogra
{
    /// <summary>
    /// klasa wszelkich obiektów strony konfliktu ( budynki, jednostki )
    /// zawiera podstawowe pola takie jak pozycja i ustawienie kątowe, oraz identyfikator strony konfliktu
    /// </summary>
    /// 
    abstract class Entity
    {
        protected struct Flags
        {

            public bool DestinationToReach;
            public bool Attacked;
            public bool Selected;
            public bool Leader;
        }

        protected Flags flags = new Flags();
        protected Vector2 position;
        protected float rotation;
        protected int team;
        protected List<Entity> enemyteam, troop = new List<Entity>();
        protected int lifetime = 0;
        protected int hp;
        protected int maxhp;
        protected ParticleSystem particleSystem = new ParticleSystem();
        protected Color color;

        public Entity(Vector2 pos, float rotation, int team, List<Entity> enemyteam)
        {
            color = Color.White;
            this.position = pos;
            this.rotation = rotation;
            this.team = team;
            this.enemyteam = enemyteam;
        }

        virtual public bool Update(TimeSpan time)
        {
            lifetime += time.Milliseconds;
            particleSystem.Update(time);

            return true;
        }

        virtual public void Draw(ExtendedSpriteBatch exSpriteBatch)
        {
            particleSystem.Draw(exSpriteBatch);

            if (Health > 0)
            {
                if (flags.Selected == true)
                    color = Color.DeepSkyBlue;
                else
                    color = Color.Gray;
                int width = 30;
                int height = 10;
                int length = (int)(width * ((float)hp / (float)maxhp));
                exSpriteBatch.FillRectangle(new Rectangle((int)(position.X - width / 2), (int)(position.Y - height), length, 3), Color.Chartreuse);
                exSpriteBatch.FillRectangle(new Rectangle((int)(position.X - width / 2 + length), (int)(position.Y - height), width - length, 3), Color.Red);
            }
            else
                color = Color.Black;
        }

        virtual public void setTargetPosition(Point mousePos)
        {
        }

        public int takehit(int hit, float attackrotation)
        {
            int tmp = (int)((float)hit * ((float)Rand.next() / 100f));
            hp -= tmp;
            flags.Attacked = true;
            particleSystem.AddEmitter(new HitEmitter(position- new Vector2((float)(20*Math.Cos(attackrotation)),(float)(20*Math.Sin(attackrotation))),attackrotation));
            
            return tmp;
        }

        #region właściwości
        public int Team
        {
            get{return team;}
            set { }
        }

        public Vector2 Position
        {
            get { return position; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
        
        public int Health 
        {
            get{return hp;}  
        }
        #endregion

        virtual public void leaderSet(List<Entity> chosen, Point mousePos)
        {

        }

        protected void lead()
        {
            float i = 0.5f;
            int j, k = 1;

            foreach (Entity asd in troop)
            {
                k = -k;
                i+=0.5f;
                j = (int)i * k;
                asd.setTargetPosition(new Point((int)(position.X + j * Math.Sin(rotation) * 30), (int)(position.Y - j * Math.Cos(rotation) * 30)));
            }
        }

        virtual public void select(bool p)
        {
            flags.Selected = p;

        }
    }
}
